package cn.itcast.oa.model;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import com.opensymphony.xwork2.ActionContext;

@Entity
@Table(name="itcast_user")
public class User implements java.io.Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Long id;
	private Department department;//所属部门
	private Set<Role> roles = new HashSet<Role>();//岗位

	private String loginName; // 登录名
	private String password; // 密码
	private String name; // 真实姓名
	private String gender; // 性别
	private String phoneNumber; // 电话号码
	private String email; // 电子邮件
	private String description; // 说明
	
	/**
	 * 判断该用户是否有权限
	 * @param privilegeName
	 * @return
	 */
	public boolean hasPrivilegeByName(String privilegeName){
		
		if(supStart()){
			return true;
		}else{
		for(Role role:roles)
			for(Privilege privilege:role.getPrivileges()){
				//判断是否有权限
				if(privilege.getName().equals(privilegeName)){
					return true;
				}
			}
		return false;
		}
	}
	
	/**
	 * 根据privilegeUrl确定权限
	 * @param privilegeUrl
	 * @return
	 */
	public boolean hasPrivilegeByUrl(String privilegeUrl){
		//超级管理员有所有的权限
		if(supStart()){
			return true;
		}		
	   if(privilegeUrl.endsWith("UI")){
		   //截取字段
		  privilegeUrl=privilegeUrl.substring(0,privilegeUrl.length()-2);
		}
	   @SuppressWarnings("unchecked")
	List<String> allPrivilegeUrl=(List<String>) ActionContext.getContext().getApplication().get("allPrivilegeUrl");
	   if(!allPrivilegeUrl.contains(privilegeUrl)){
		   /*contains方法是判断是否包含字段
		    * 所有的权限中没有的权限则所有的人都有权限*/
		   return true;
	   }else{
		for(Role role:roles)
			for(Privilege privilege:role.getPrivileges()){
				//判断是否有权限
				if(privilegeUrl.equals(privilege.getUrl())){
					return true;
				}
			}
		return false;
	   }
	}
	
	public boolean supStart() {
		// TODO Auto-generated method stub
		
	  return loginName.equals("admin");
	}

	@Id//主键
	@GeneratedValue//自增长
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	
	//user与部门是多对一的
  
    @ManyToOne//多对一加在多的一方
    @JoinColumn(name="departmentId")//起数据库名字段名
	public Department getDepartment() {
		return department;
	}
	public void setDepartment(Department department) {
		this.department = department;
	}
	
	/**
	 * fetch=fetchType取数据不是懒加载
	 * user与岗位是多对多，要一张中间表
	 * @return
	 */
	@ManyToMany(fetch=FetchType.EAGER)
	@JoinTable(name="itcast_user_role",
    joinColumns={@JoinColumn(name="user_id")},
		inverseJoinColumns={@JoinColumn(name="role_id")}
     )
	public Set<Role> getRoles() {
		return roles;
	}
	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}
	public String getLoginName() {
		return loginName;
	}
	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	public String getPhoneNumber() {
		return phoneNumber;
	}
	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	
}
